package com.example.jyapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jyapplication.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PersonUpdateActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "Update";
    EditText etUpdatePassword,etUpdateNewPassword;
    Button btnUpdateCancel,btnUpdateConfirm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_update);
        initView();
    }

    private void initView() {
        etUpdatePassword = findViewById(R.id.et_update_password);
        etUpdateNewPassword = findViewById(R.id.et_update_newpassword);
        btnUpdateCancel = findViewById(R.id.btn_update_back);
        btnUpdateCancel.setOnClickListener(this);
        btnUpdateConfirm = findViewById(R.id.btn_update_confirm);
        btnUpdateConfirm.setOnClickListener(this);
    }

    private void initEvent() {
        String password = etUpdatePassword.getText().toString();
        final String newPassword = etUpdateNewPassword.getText().toString();

        SharedPreferences sp = this.getSharedPreferences("info",0);
        final String userName = sp.getString("userName","");
        String userPassword = sp.getString("userPassword","");

        if (password.equals(userPassword)){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OkHttpClient client = new OkHttpClient();
                    String url = "http://10.20.246.109:8080/18/All/update?password="+newPassword+"&telephone="+userName;
                    FormBody body = new FormBody.Builder().build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(body)
                            .build();
                    Call call = client.newCall(request);
                    try {
                        Response response = call.execute();
                        final String data = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (data.equals("\"成功\"")){
                                    SharedPreferences sp = PersonUpdateActivity.this.getSharedPreferences("info",0);
                                    SharedPreferences.Editor editor = sp.edit();
                                    editor.putString("userPassword",newPassword);
                                    editor.commit();
                                    Toast.makeText(PersonUpdateActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                                    Intent toLogin = new Intent(PersonUpdateActivity.this,LoginActivity.class);
                                    startActivity(toLogin);
                                }else {
                                    Toast.makeText(PersonUpdateActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else if (etUpdatePassword.getText().toString().equals("")||etUpdateNewPassword.getText().toString().equals("")){
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_update_confirm:
                initEvent();
                break;
            case R.id.btn_update_back:
                Intent intent = new Intent(this,PersonActivity.class);
                startActivity(intent);
                break;
        }
    }
}